package com.ccy_blog.back.service.impl;

import com.ccy_blog.back.bean.Article;
import com.ccy_blog.back.bean.Comment;
import com.ccy_blog.back.bean.Category;
import com.ccy_blog.back.mapper.CommentMapper;
import com.ccy_blog.back.service.CommentService;
import com.ccy_blog.base.exception.BlogEnum;
import com.ccy_blog.base.exception.BlogException;
import com.ccy_blog.base.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Override
    public Comment queryById(String id) {
        return null;
    }

    @Override
    public List<Comment> list(String uid) {
        List<Comment> comments;
        System.out.println("uidasd"+uid);
        if(uid.equals("1")){
             comments = commentMapper.selectAll();
        }else{
            Example example = new Example(Article.class);
            Example.Criteria criteria = example.createCriteria();
            //查询当前登录用户的文章
            criteria.andEqualTo("uid",uid);
             comments = commentMapper.selectByExample(example);
        }

        return comments;
    }

    @Override
    public void deleteById(String cid) {
        int count = commentMapper.deleteByPrimaryKey(cid);
        if(count == 0){
            throw new BlogException(BlogEnum.COMMENT_DELETE);
        }
    }

    @Override
    public Boolean save(Comment comment) {
        comment.setCreate_time(DateTimeUtil.getSysTime());
        int count = commentMapper.insert(comment);
        if(count == 0){
            throw new BlogException(BlogEnum.COMMENT_PUNISH);
        }
        return true;
    }
}
